/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package warehouse;

import ejb.warehouse.Articolo;
import ejb.warehouse.ArticoloFacadeRemote;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import system.Utilities;




/**
 *
 * @author ciummo
 */
public class ArticleAction extends DispatchAction {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";
    private static final String TONEWARTICLE = "toNewArticle";
    private static final String SUCCESSARTICLE = "successArticle";
    private static final String TOARTICLESLIST = "toArticlesList";
    private static final String EDITARTICLE = "editArticle";
    
    
    private Utilities utility = new Utilities();
    
    
    public ActionForward newArticle(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ArticleActionForm articleForm = (ArticleActionForm) form;
        
        articleForm.reset(mapping, request);
        
        return mapping.findForward(TONEWARTICLE);
    }
    
    public ActionForward editArticle(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
               
        String codArticle = request.getParameter("codArticolo");
        ArticoloFacadeRemote articoloFacade = (ArticoloFacadeRemote) utility.callRemoteBean("ArticoloFacade/remote");
                
        ArticoloBeanToArticleForm toForm = new ArticoloBeanToArticleForm();
        
        ArticleActionForm articleForm = toForm.convert(articoloFacade.find(codArticle));
        
        request.getSession().setAttribute("ArticleActionForm", articleForm);
        
        return mapping.findForward(EDITARTICLE);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2, where "method" is the value
     * specified in <action> element : ( <action parameter="method" .../> )
     */
    public ActionForward submitArticle(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ArticleActionForm articleForm = (ArticleActionForm) form;
        
        String method = request.getParameter("func");
        
        if (method.equalsIgnoreCase("new")){
        
            ActionErrors errors = form.validate(mapping, request);

            if (!errors.isEmpty()) {
                saveErrors(request, errors);
                return  mapping.findForward(TONEWARTICLE);
            }
        }
        
        
        ArticoloFacadeRemote articoloFacade = (ArticoloFacadeRemote) utility.callRemoteBean("ArticoloFacade/remote");
        
        Articolo articolo = new Articolo();
        
        
        articleForm.getListaTipi();
        
        
        articolo.setCodArticolo(articleForm.getCodArticolo());
        articolo.setNome(articleForm.getNome());
        articolo.setTipo(articleForm.getTipo());
        articolo.setPrezzo(articleForm.getPrezzo());
        articolo.setNote(articleForm.getNote());
        
        if (method.equalsIgnoreCase("new")){
            
            articoloFacade.create(articolo);
        }
        
        else {
            
            articoloFacade.edit(articolo);
        }
        
        request.getSession().setAttribute("idArticle", articleForm.getCodArticolo());
        
        
        return mapping.findForward(SUCCESSARTICLE);
    }
    
    public ActionForward listArticles(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        
            String method = request.getParameter("func");
            
            ArticoloFacadeRemote articoloFacade = (ArticoloFacadeRemote) utility.callRemoteBean("ArticoloFacade/remote");
           
            ArticleActionForm articleForm = (ArticleActionForm) form;
            
            try{
                if (!method.equalsIgnoreCase("all") && !method.equalsIgnoreCase("new")){

                    articleForm.setList(articoloFacade.findArticoliByType(method));
                    request.getSession().setAttribute("view", method);
                    return mapping.findForward(TOARTICLESLIST);
                }
            }catch(Exception e){}
            
            articleForm.setList(articoloFacade.findAll());
            
            
            request.getSession().setAttribute("view", "all");
            
            return mapping.findForward(TOARTICLESLIST);
    }        
    
    public ActionForward eraseArticle(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String codArticle = request.getParameter("codArticolo");
        ArticoloFacadeRemote articoloFacade = (ArticoloFacadeRemote) utility.callRemoteBean("ArticoloFacade/remote");
        
        articoloFacade.remove(articoloFacade.find(codArticle));
        
        
        return mapping.findForward(SUCCESSARTICLE);
    }
    
}

class ArticoloBeanToArticleForm {
    
    
    public ArticleActionForm convert (Articolo articolo){
        
        ArticleActionForm articleForm = new ArticleActionForm();      
        
        articleForm.setCodArticolo(articolo.getCodArticolo());
        articleForm.setNome(articolo.getNome());
        articleForm.setTipo(articolo.getTipo());
        articleForm.setPrezzo(articolo.getPrezzo());
        articleForm.setNote(articolo.getNote());
        
        return articleForm;
    }
    
}